<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2024/1/5 14:29
 * Desc: 订单完成处理-（处理父子订单）
 */
class Listener_Order_Finished_Order extends Listener_Base
{
    public function handle($args)
    {
        $self       = new self();
        $self->args = $args;
        $order      = $args['order'];
        // 目前只处理景点订单，后续根据需求再扩展
        if ($order['typeid'] == 5 && ($order['haschild'] == 1 || $order['pid'])) {
            if ($self->queue_status) {
                //推送到队列执行
                Queue::in('default', Listener_Order_Finished_Order::class, $args);
            } else {
                $self->perform();
            }
        }
    }

    public function perform()
    {
        $args  = $this->args;
        $order = $args['order'];
        $msg   = '处理父子订单';

        if ($order['haschild']) {
            // 有子订单
            $child_order = DB::select()
                ->from('member_order')
                ->where('pid', '=', $order['id'])
                ->and_where('status', '=', '2')
                ->execute()
                ->as_array();
            if (!empty($child_order)) {
                // 有待消费的子订单，则消费子订单
                foreach ($child_order as $v) {
                    $this->consume_order($v['id']);
                }
                foreach ($child_order as $item) {
                    $product_model  = St_Product::get_product_model($item['typeid']);
                    $item['status'] = 5;
                    Model_Member_Order::back_order_status_changed(2, $item, $product_model, '自动消费');
                }
                $msg = '有待消费的子订单，消费子订单';
            } else {
                $msg = '没有待消费的子订单，不用消费子订单';
            }
        } else if ($order['pid']) {
            // 有父级订单, 查询是否有子级订单
            $child_order = DB::select()
                ->from('member_order')
                ->where('pid', '=', $order['pid'])
                ->and_where('status', '=', '2')
                ->execute()
                ->as_array();
            if (empty($child_order)) {
                // 没有待消费的子订单，则消费父级订单
                $this->consume_order($order['pid'], true);
                $msg = '没有待消费的子订单，消费父级订单';
            } else {
                $msg = '有待消费的子订单，不消费父级订单';
            }
        }

        St_Log::write('订单完成', array('ordersn' => $order['ordersn'], 'msg' => $msg), 'event/order_finished/log');
    }

    // 消费订单
    private function consume_order($order_id, $status_changed = false)
    {
        $order = ORM::factory('member_order')
            ->where('id', '=', $order_id)
            ->and_where('status', '=', '2')
            ->find();
        if (!empty($order->id)) {
            $order->status = 5;
            $order->save();
            if ($order->saved()) {
                $product_model  = St_Product::get_product_model($order->typeid);
                $order->reload();
                $new_order = DB::select()
                    ->from('member_order')
                    ->where('pid', '=', $order_id)
                    ->and_where('status', '=', '2')
                    ->execute()
                    ->as_array();
                if ($status_changed) {
                    Model_Member_Order::back_order_status_changed(2, $new_order, $product_model, '自动消费');
                }
            }
        }
    }
}